<style>
.con {
  margin-top: 50px;
  /* display: flex;
    justify-content: center;
    align-items: center; */
  /* margin-top: 100px; */
  /* border: 1px solid #ccc; */
}
</style>
<template>
  <div class="con">
    <el-form ref="form" :model="proNewProcure" :rules="rules" label-width="150px">
      <el-col :span="8">
        <el-form-item label="货号" prop="productNo">
          <el-input v-model="proTemplate.productNo" placeholder="请输入货号" :disabled="true" />
        </el-form-item>
        <el-form-item label="产品图" prop="picUrl">
          <image-preview :src="proTemplateSecondry.picUrl" :width="80" :height="80" />
        </el-form-item>
        <!-- //-----------新增或修改方法里上传图片 -->
        <!-- <el-form-item label="图片地址" prop="picUrl">
                <el-upload :action="imgUpload.url" :headers="imgUpload.headers" list-type="picture-card" :limit="limit"
                                                                                                                                                                                                    :on-exceed="handleExceed" :on-success="handlePictureSuccessPic" :before-upload="beforeAvatarUpload"
                                                                                                                                                                                                                                                                    :on-preview="handlePictureCardPreview" :file-list="files">
                                                                                                                                                                                                                                                                    <i class="el-icon-plus"></i>
                                                                                                                                                                                                                                                                </el-upload>
                                                                                                                                                                                                        <el-dialog :visible.sync="dialogVisible">
                                                                                                                                                                                                            <img width="100%" v-if="picUrl" :src="picUrl" alt="">
                                                                                                                                                                                                        </el-dialog>
                                                                                                                                                                                                    </el-form-item> -->
        <el-form-item label="颜色属性" prop="colorProperty">
          <el-input v-model="proTemplateSecondry.colorProperty" placeholder="请输入颜色属性" :disabled="true" />
        </el-form-item>
        <!-- <el-form-item label="尺寸" prop="proTemplateSizeList">
                                                                        <el-checkbox-group v-model="checked" @change="test">
                                                                            <el-checkbox v-for="item in checkList" :key="item.id" :label="item.id" :disabled="true">{{
                        item.label }}
                        </el-checkbox>
                    </el-checkbox-group>

                    
                </el-form-item> -->
        <!-- v-show="scope.row.proTemplateSecondry.logoUrl" -->
        <!-- <el-form-item label="logo图片" prop="logoUrl">
                    <template #default="scope">
                        <el-popover effect="light" trigger="hover" placement="right" width="auto">
                            <template #default>
                                <img :src='pjtUrl + proTemplateSecondry.logoUrl'
                                    style="object-fit: contain;width: 300px;height: 200px;">
                                                                                    </template>
                                                                                    <template #reference>
                            <img :src='pjtUrl + proTemplateSecondry.logoUrl'
                                style="object-fit: contain;width: 100px; height: 100px" />
                        </template>
                    </el-popover>
                </template>
            </el-form-item> -->
        <!-- //-----------新增或修改方法里上传图片 -->
        <!-- <el-form-item label="logo图片" prop="logoUrl">
                <el-upload :action="imgUpload.url" :headers="imgUpload.headers" list-type="picture-card" :limit="limit"
                                                                                                                                                                                                    :on-exceed="handleExceed" :on-success="handlePictureSuccess" :before-upload="beforeAvatarUpload"
                                                                                                                                                                                                            :on-preview="handlePictureCardPreview" :file-list="fileList">
                                                                                                                                                                                                            <i class="el-icon-plus"></i>
                                                                                                                                                                                                                                                                </el-upload>
                                                                                                                                                                                                                                                                <el-dialog :visible.sync="dialogVisible">
                                                                                                                                                                                                                                                                    <img width="100%" v-if="imageUrl" :src="imageUrl" alt="">
                                                                                                                                                                                                                                                                </el-dialog>
                                                                                                                                                                                                                                                            </el-form-item> -->
        <!-- <el-form-item label="生产厂家代码" prop="manufacturerCode">
                                                                            <el-input v-model="proTemplate.manufacturerCode" placeholder="请输入生产厂家代码" :disabled="true" />
                                                                        </el-form-item> -->
        <el-form-item label="运营-计划上架时间" prop="operationPlanTime">
          <el-date-picker clearable v-model="proNewRelation.operationPlanTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择运营-计划上架时间" :disabled="true">
          </el-date-picker>
        </el-form-item>
        <!-- ；0=否；1=是 -->
        <el-form-item label="运营-京东-是否上架" prop="operationJingdongUploaded">
          <!-- <el-input :disabled="true" v-model="proNewRelation.operationJingdongUploaded"
                                                                                                                                                    placeholder="请输入运营-京东-是否上架；0=否；1=是" /> -->
          <el-radio-group v-model="proNewRelation.operationJingdongUploaded" :disabled="true">
            <el-radio v-for="dict in dict.type.sys_one_zero" :key="dict.value" :label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="运营-京东-主推款" prop="operationJingdongMain">
          <el-input :disabled="true" v-model="proNewRelation.operationJingdongMain" placeholder="请输入运营-京东-主推款" />
        </el-form-item>
        <el-form-item label="采购出货时间" prop="shippingTime">
          <el-date-picker :disabled="true" clearable v-model="proNewProcure.shippingTime" type="date"
            value-format="yyyy-MM-dd" placeholder="请选择出货时间">
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="2"> <el-form-item></el-form-item></el-col>
      <el-col :span="8">
        <el-form-item label="商品-品牌信息" prop="merchandiseBrand">
          <!-- <el-input v-model="proTemplate.merchandiseBrand" placeholder="请输入商品-品牌信息" /> -->
          <el-select v-model="proTemplate.merchandiseBrand" placeholder="请选择品牌信息" clearable>
            <el-option v-for="dict in dict.type.sys_product_pp" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="商品-季节" prop="productSeason">
          <!-- <el-input v-model="proTemplate.productSeason" placeholder="请输入商品-季节" /> -->
          <el-select v-model="proTemplate.productSeason" placeholder="请选择商品-季节" clearable>
            <el-option v-for="dict in dict.type.sys_product_jijie" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>


        <el-form-item label="商品-销售价格" prop="merchandiseSalePrice">
          <el-input v-model="proNewRelation.merchandiseSalePrice" placeholder="请输入商品-销售价格" />
        </el-form-item>
        <el-form-item label="商品-吊牌价格" prop="merchandiseTagPrice">
          <el-input v-model="proNewRelation.merchandiseTagPrice" placeholder="请输入商品-吊牌价格" />
        </el-form-item>
        <!--   @change="changeFirst" -->
        <el-form-item label="一级类目" prop="firstLevelColumn">
          <!-- <el-select v-model="proNewRelation.firstLevelColumn" placeholder="请选择一级类目" clearable>
                                                                    <el-option v-for="dict in firstList" :key="dict.dictValue" :label="dict.dictLabel"
                                                                        :value="dict.dictValue" />
                                                                </el-select> -->
          <!-- :disabled="true" -->
          <!-- <el-input v-model="proTemplate.firstLevelColumn" placeholder="请输入一级栏目" />  @change="changeFirst"-->
          <el-select v-model="proTemplate.firstLevelColumn" placeholder="请选择一级类目" clearable @change="changeFirst">
            <el-option v-for="dict in dict.type.sys_product_pl" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="二级类目" prop="firstLevelExtendedColumn">
          <!-- <el-input v-model="form.firstLevelColumn" placeholder="请输入一级栏目" /> -->
          <el-select v-model="proTemplate.firstLevelExtendedColumn" placeholder="请选择二级类目" clearable filterable>
            <el-option v-for="dict in dict.type.sys_product_secondary" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="三级类目" prop="secondLevelColumn">
          <el-input v-show="this.pj" v-model="proTemplate.secondLevelColumn" placeholder="请输入三级类目" />
          <!-- <el-input v-model="proTemplate.secondLevelColumn" placeholder="请输入三级类目" /> -->
          <!--  :disabled="true" @change="changesecond"-->
          <el-select v-show="!this.pj" v-model="proTemplate.secondLevelColumn" placeholder="请选择三级类目" filterable clearable
            @change="changesecond">
            <el-option v-for="dict in dict.type.sys_product_cycle" :key="dict.label" :label="dict.label"
              :value="dict.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="商品-主推款" prop="operationMainProducts">
          <el-input v-model="proTemplate.operationMainProducts" placeholder="请输入运营-主推款" />
        </el-form-item>
        <!-- <el-form-item label="商品计划上架时间" prop="merchandisePlanTime">
                                                                                                                                <el-date-picker clearable v-model="proNewRelation.merchandisePlanTime" type="date"
                                                                                                                                    value-format="yyyy-MM-dd" placeholder="请选择商品计划上架时间">
                                                                                                                                </el-date-picker>
                                                                                                                            </el-form-item> -->
        <el-form-item label="商品-计划上架时间-备注" prop="merchandiseTagPrice" label-width="170px">
          <el-input v-model="proNewRelation.merchandisePlanTimeMark" placeholder="请输入商品-备注" />
        </el-form-item>
        <div class="footer" style="text-align: center">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel" style="margin-left: 30px">返回上一级</el-button>
        </div>
      </el-col>
    </el-form>
  </div>
</template>
<script>
import {
  listData,
  getData,
  delData,
  addData,
  updateData,
} from "@/api/system/dict/data";
// import { listData, getData, getDict, delData, addData, updateData } from "@/api/data/data";
import {
  listRelation,
  getRelation,
  delRelation,
  addRelation,
  updateRelation,
} from "@/api/product/relation";
import {
  listPurchase,
  getPurchase,
  delPurchase,
  addPurchase,
  updatePurchase,
} from "@/api/edit/purchase";
//--------导入token
import { getToken } from "@/utils/auth";
export default {
  // dicts: [],
  name: "Purchase",
  dicts: [
    "sys_one_zero",
    "sys_product_qudao",
    "sys_product_size",
    "sys_product_design",
    "sys_product_scene",
    "sys_product_coder",
    "sys_product_yf",
    "sys_product_cycle",
    "sys_product_pl",
    "sys_product_pp",
    "sys_product_jijie",
    "sys_product_secondary",
  ],
  data() {
    return {
      //配件
      pj: false,
      //一级类目
      firstList: [],
      proNewRelation: {},
      //图片相对路径
      picurl: "",
      //图片地址
      picUrl: "",
      //logo相对路径
      logoUrl: "",
      pjtUrl: process.env.VUE_APP_BASE_API,
      // 图片数量限制
      limit: 1,
      //页面上存的暂时图片地址List
      files: [{ url: "" }],
      //页面上存的暂时logo图片地址List
      fileList: [{ url: "" }],
      //图片地址
      imageUrl: "",
      dialogVisible: false,
      imgUpload: {
        // 设置上传的请求头部
        headers: {
          Authorization: "Bearer " + getToken(),
        },
        // 图片上传的方法地址:
        url: process.env.VUE_APP_BASE_API + "/common/upload",
      },
      checked: [],
      checkList: [
        { id: 1, label: "XS" },
        { id: 2, label: "S" },
        { id: 3, label: "M" },
        { id: 4, label: "L" },
        { id: 5, label: "XL" },
        { id: 6, label: "XXL" },
        { id: 7, label: "均码" },
        // { id: 0002, label: '汽车' },
        // { id: 0002, label: '火车' },
      ],
      // pjtUrl: process.env.VUE_APP_BASE_API,
      proTemplateSecondry: {}, //二级
      proTemplate: {}, //一级
      // 表单参数
      form: {
        // proNewProcure:
      },
      proNewProcure: {},
      // 表单校验
      rules: {},
    };
  },

  created() {
    // this.getList();
    this.handleUpdate();
    // this.getFirst()
  },
  methods: {
    changesecond(val) {
      console.log(JSON.stringify(val));
      listData({ dictType: "sys_product_cycle", dictLabel: val }).then(
        (response) => {
          // this.dataList = ;
          console.log(JSON.stringify(response.rows.dictValue));

          this.form.proTemplate.workshopCycle = response.rows[0].dictValue;
          console.log(JSON.stringify(response.rows));
          // this.total = response.total;
          // this.loading = false;
        }
      );
    },
    //changeFirst 改变一级类目
    changeFirst(val) {
      console.log(val);
      if (val == "配件") {
        this.pj = true;
      } else {
        this.pj = false;
      }
    },
    //获取一级类目
    getFirst() {
      getDict("sys_product_first").then((res) => {
        console.log(JSON.stringify(res));
        this.firstList = res.data;
      });
    },
    test() {
      this.$modal.msgError("禁止修改尺寸");
    },
    /** 查询产品采购信息列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then((response) => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.$router.go(-1);
      return;
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.files = undefined; //加的是这一行
      this.fileList = undefined; //加的是这一行
      this.proNewProcure = {
        id: null,
        proNewId: null,
        orderNum: null,
        orderTime: null,
        fabricCycle: null,
        workshopCycle: null,
        shippingTime: null,
        fabricComposition: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id || this.ids
      const id = this.$route.query.id;
      if (!id) {
        this.$modal.msgError("请点击左侧菜单-上新管理进入");
        return;
      }
      getRelation(id).then((response) => {
        this.proNewRelation = response.data.proNewRelation;
        if (response.data.proNewProcure) {
          this.proNewProcure = response.data.proNewProcure;
        }

        //二级
        this.proTemplateSecondry = response.data.proTemplateSecondry;
        this.picurl = response.data.proTemplateSecondry.picUrl;
        this.logoUrl = response.data.proTemplateSecondry.logoUrl;
        console.log(JSON.stringify(this.proTemplateSecondry));
        //一级
        this.proTemplate = response.data.proTemplate;
        this.form = response.data;
        console.log(JSON.stringify(this.form));
        this.open = true;
        this.title = "修改产品采购信息";
        //图片回显
        this.files = [
          {
            url:
              process.env.VUE_APP_BASE_API +
              response.data.proTemplateSecondry.picUrl,
          },
        ]; //图片加的是这一行
        this.fileList = [
          {
            url:
              process.env.VUE_APP_BASE_API +
              response.data.proTemplateSecondry.logoUrl,
          },
        ]; //logo图片加的是这一行
        //尺寸回显

        if (
          response.data.proNewVO.proNewSizeList &&
          response.data.proNewVO.proNewSizeList.length > 0
        ) {
          let val = response.data.proNewVO.proNewSizeList.map((item) => {
            return item.size;
          });
          console.log(JSON.stringify(val));
          let m = [];
          for (let i = 0; i < val.length; i++) {
            // const element = array[i];
            this.checkList.forEach((item) => {
              if (val[i] == item.label) {
                m.push(item.id);
              }
            });
          }

          console.log(JSON.stringify(m));
          this.checked = m;
          // this.$set(this.form, 'checked', m)
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      // this.form.proTemplateSecondry.picUrl = this.picurl
      // this.form.proTemplateSecondry.logoUrl = this.logoUrl; // 注：重要(用于添加到数据库)，
      this.form.proNewProcure = this.proNewProcure;
      // this.form.proTemplateSecondry = this.proTemplateSecondry
      console.log(JSON.stringify(this.form));

      updateRelation(this.form).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        console.log(JSON.stringify(response));
        this.handleUpdate();
      });
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
          } else {
            // addPurchase(this.form).then(response => {
            //     this.$modal.msgSuccess("新增成功");
            //     this.open = false;
            //     this.getList();
            // });
          }
        }
      });
    },
    //图片上传前的相关判断
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type == "image/png";
      const isLt2M = file.size / 1024 / 1024 < 5;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG/PNG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 5MB!");
      }
      return isJPG && isLt2M;
    },
    //图片预览
    handlePictureCardPreview(file) {
      this.picUrl = file.url;
      this.imageUrl = file.url;
      this.dialogVisible = true;
    },
    handlePictureSuccessPic(res, file) {
      //设置图片访问路径 （url 后台传过来的的上传地址）
      this.picUrl = file.response.url;
      // const url = file.response.fileName;
      this.picurl = file.response.fileName;
    },
    //图片上传成功后的回调
    handlePictureSuccess(res, file) {
      //设置图片访问路径 （url 后台传过来的的上传地址）
      this.imageUrl = file.response.url;
      // const url = file.response.fileName;
      this.logoUrl = file.response.fileName;
      // console.log(JSON.stringify(url));
    },

    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`上传链接LOGO图片数量不能超过 ${this.limit} 个!`);
    },
  },
};
</script>